﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HandBook.Data;
using Telerik.Web.UI;
using System.Data;

public partial class Admin_StyleManagement : BasePage
{
    public bool isDuplicate = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["holdstate"] != null)
        {
            uxStyle.DataSource = ViewState["holdstate"];
        }
        RadComboBox1.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + btnSearch.UniqueID + "','')");
    }
    protected void uxStyle_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        var styleList = from list in HandBookData.EatDrinkDB.Styles
                        select list;
        uxStyle.DataSource = styleList;
    }
    protected void RadComboBox1_DataBound(object sender, EventArgs e)
    {
        RadComboBox1.Text = "Nhập vào từ khóa cần tìm";
    }
    protected void uxStyle_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int styleID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["StyleID"]);

        var style = HandBookData.EatDrinkDB.Styles.Single(p => p.StyleID == styleID);
        string newStyleName = (e.Item.FindControl("txtStyleName") as TextBox).Text.Trim();

        //kiểm tra xem styleName đã có ở csdl hay chưa
        var styleList = from s in HandBookData.EatDrinkDB.Styles
                        where s.StyleID != styleID
                        select s;
        foreach (var item in styleList)
        {
            if (item.StyleName.Equals(newStyleName))
            {
                isDuplicate = true;
                break;
            }
        }

        //thực hiện việc kiểm tra và update
        if (isDuplicate)
        {
            //Response.Write("<script>return false;</script>");
            e.Canceled = true;
            //Page.RegisterStartupScript("abc", "<script>alert('loi');</script>");
            (e.Item.FindControl("lbError") as Label).Visible = true;
        }
        else
        {
            style.StyleName = newStyleName;
            HandBookData.EatDrinkDB.SubmitChanges();
        }
    }
    protected void uxStyle_InsertCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem insertItem = e.Item as GridEditableItem;
        HandBook.Data.Style st = new HandBook.Data.Style();
        st.StyleName = (e.Item.FindControl("txtStyleName") as TextBox).Text.Trim();

        //kiểm tra xem styleName đã có ở csdl hay chưa
        var styleList = from s in HandBookData.EatDrinkDB.Styles
                        select s;
        foreach (var item in styleList)
        {
            if (item.StyleName.Equals(st.StyleName))
            {
                isDuplicate = true;
                break;
            }
        }

        if (!isDuplicate)
        {
            HandBookData.EatDrinkDB.Styles.InsertOnSubmit(st);
            HandBookData.EatDrinkDB.SubmitChanges();
        }
        else
        {
            //Response.Write("<script>return false;</script>");
            e.Canceled = true;
            (e.Item.FindControl("lbError") as Label).Visible = true;
        }
    }
    
    protected void uxStyle_ItemCreated(object sender, GridItemEventArgs e)
    {
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        EatDrinkDBDataContext context = new EatDrinkDBDataContext();

        string content = RadComboBox1.Text.Trim();
        string specialChar = "_+*:,.@{}[]()~$#%^&=|;?/<>!";
        int len = specialChar.Length;
        specialChar = specialChar.Trim();

        //lọc bỏ các ký tự đặc biệt
        for (int i = 0; i < len; i++)
        {
            if (content.Contains(specialChar[i]))
            {
                content = content.Replace(specialChar[i].ToString(), "");
            }
        }

        //tách chuỗi tìm kiếm
        string[] keywords = content.Split(' ');
        string where = string.Empty;
        foreach (string item in keywords)
        {
            if (where.Equals(string.Empty))
            {
                where = "StyleName LIKE '%" + item + "%'";
            }
            else
            {
                where += " OR StyleName LIKE '%" + item + "%'";
            }
        }
        string sql = "SELECT * FROM Style WHERE " + where;

        var styleList = context.ExecuteQuery<HandBook.Data.Style>(sql);

        DataTable table = new DataTable();
        table.Columns.Add("StyleID");
        table.Columns.Add("StyleName");

        foreach (var item in styleList)
        {
            DataRow row = table.NewRow();
            row["StyleID"] = item.StyleID;
            row["StyleName"] = item.StyleName;
            table.Rows.Add(row);
        }

        ViewState["holdstate"] = table;

        uxStyle.DataSource = table;
        uxStyle.Rebind();
    }
    protected void uxStyle_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "ViewAll")
        {
            var styleList = from r in HandBookData.EatDrinkDB.Styles
                           select r;

            DataTable table = new DataTable();
            table.Columns.Add("StyleID");
            table.Columns.Add("StyleName");

            foreach (var item in styleList)
            {
                DataRow row = table.NewRow();
                row["StyleID"] = item.StyleID;
                row["StyleName"] = item.StyleName;
                table.Rows.Add(row);
            }

            ViewState["holdstate"] = table;

            uxStyle.DataSource = table;
            uxStyle.Rebind();
        }
    }
}